package Algorithms;

/**
 * 使用算法
 * @author 车宗绍Che Zongshao
 * @date 2015/1/7
 */

import utils.Edge;
import utils.Graph;
import utils.Node;

public class UseAlgorithms {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// 初始化图
		initialize();
		
		// 使用A*算法
		Node[] result = AStar.doAStar();
		System.out.print("A*算法结果：");
		for (int i = 0; i < result.length; i++) {
			int i_name = result[i].getName();
			String s_name = new String();
			switch(i_name) {
				case 1: s_name = "A"; break;
				case 2: s_name = "B"; break;
				case 3: s_name = "C"; break;
				case 4: s_name = "D"; break;
				case 5: s_name = "E"; break;
				default: s_name = "error";
			}
			System.out.print(s_name);
			if (i < result.length - 1) {
				System.out.print("->");
			} else {
				System.out.println();
			}
		}
		// 使用GA算法
		Node[] resultGA = new GA().doGA();
		System.out.print("GA算法结果：");
		for (int i = 0; i < resultGA.length; i++) {
			int i_name = resultGA[i].getName();
			String s_name = new String();
			switch(i_name) {
				case 1: s_name = "A"; break;
				case 2: s_name = "B"; break;
				case 3: s_name = "C"; break;
				case 4: s_name = "D"; break;
				case 5: s_name = "E"; break;
				default: s_name = "error";
			}
			System.out.print(s_name);
			if (i < resultGA.length - 1) {
				System.out.print("->");
			}
		}
	}
	
	/**
	 * 初始化参数
	 * @return Graph对象
	 */
	private static Graph initialize() {
		// nodes
		// root node
		Node A = new Node(1);
		// others node
		Node B = new Node(2);
		Node C = new Node(3);
		Node D = new Node(4);
		// target node
		Node E = new Node(5);
		
		// edges
		Edge edgeAB = new Edge(7, A, B);
		Edge edgeAC = new Edge(5, A, C);
		Edge edgeAD = new Edge(10, A, D);
		Edge edgeAE = new Edge(13, A, E);
		
		Edge edgeBA = new Edge(7, B, A);
		Edge edgeBC = new Edge(7, B, C);
		Edge edgeBD = new Edge(10, B, D);
		Edge edgeBE = new Edge(10, B, E);
		
		Edge edgeCA = new Edge(5, C, A);
		Edge edgeCB = new Edge(7, C, B);
		Edge edgeCD = new Edge(5, C, D);
		Edge edgeCE = new Edge(9, C, E);
		
		Edge edgeDA = new Edge(10, D, A);
		Edge edgeDB = new Edge(10, D, B);
		Edge edgeDC = new Edge(5, D, C);
		Edge edgeDE = new Edge(6, D, E);
		
		
		// 生成graph
		Node root = A;
		root.setNextEdges(new Edge[] {edgeAB, edgeAC, edgeAD, edgeAE});
		Node target = E;
		Node[] nodes = new Node[] {B, C, D, E};
		Edge[] edges = new Edge[] {edgeBA, edgeBC, edgeBD, edgeBE, 
				edgeCA, edgeCB, edgeCD, edgeCE, 
				edgeDA, edgeDB, edgeDC, edgeDE};
		return new Graph(root, target, nodes, edges);
	}

}